# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import Dict, Any
from pydantic import BaseModel, ValidationError

class SumArgs(BaseModel):
    a: float
    b: float

def sum_tool(**kwargs) -> float:
    args = SumArgs(**kwargs)
    return float(args.a) + float(args.b)

TOOL_REGISTRY = {
    "sum": sum_tool,
}

def dispatch_tool(name: str, args: Dict[str, Any]) -> Any:
    if name not in TOOL_REGISTRY:
        raise KeyError(f"未知工具: {name}")
    fn = TOOL_REGISTRY[name]
    try:
        return fn(**args)
    except ValidationError as ve:
        # 透出更友好的错误信息给上层
        raise ValueError(f"参数校验失败: {ve}") from ve
